package br.com.futibamanager.dao;

import java.util.List;

import br.com.futibamanager.model.User;

import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.query.Predicate;

/**
 * User DAO class.
 * 
 * @author diogenes.feijo
 *
 */
public class UserDAO extends DAO {
	
	/**
	 * Finds users by its login.
	 *  
	 * @param login - the login
	 * 
	 * @return List
	 */
	public List<User> findByLogin(final String login) {
		
		ObjectContainer objectContainer = getObjectContainer();
		
		List<User> userList = objectContainer.query(new Predicate<User>() {

			private static final long serialVersionUID = 1L;

			public boolean match(User matchUser) {
				return matchUser.getLogin().equals(login);
			}
		});
		
		return userList;
	}
	
	/**
	 * Finds a user by its e-mail.
	 * 
	 * @param email - user's e-mail
	 * 
	 * @return User
	 */
	public User findByEmail(final String email) {
		
		ObjectContainer objectContainer = getObjectContainer();
		
		ObjectSet<User> userList = objectContainer.query(new Predicate<User>() {

			private static final long serialVersionUID = 1L;

			public boolean match(User matchUser) {
				return matchUser.getEmail().equals(email);
			}
		});
		
		User user = null;
		
		if (userList != null && !userList.isEmpty()) {
			user = userList.next();
		}
		
		return user;
	}
	
	/**
	 * Finds a user by its login and password.
	 * 
	 * @param login - user's login
	 * @param password - user's password
	 * 
	 * @return User
	 */
	public User findByLoginAndPassword(final String login, final String password) {
		
		ObjectContainer objectContainer = getObjectContainer();
		
		ObjectSet<User> userList = objectContainer.query(new Predicate<User>() {

			private static final long serialVersionUID = 1L;

			public boolean match(User matchUser) {
				return matchUser.getLogin().equals(login)
							&& matchUser.getPassword().equals(password);
			}
		});
		
		User user = null;
		
		if (userList != null && !userList.isEmpty()) {
			user = userList.next();
		}
		
		return user;
	}

}